import { detailProcess } from '@/api/workflow/process'
import { findTreeNodeForFSG } from '@/utils/obj-util'

export default {
  methods: {
    getProcessDetails(procInsId, taskId, routerType = '1') {
      const params = { procInsId: procInsId, taskId: taskId }
      detailProcess(params).then(res => {
        const data = res.data;
        this.xmlData = data.bpmnXml;
        let tempProcessFormList = data.processVFormList
        if (tempProcessFormList && tempProcessFormList.length) {
          console.log('getProcessDetails tempProcessFormList: ', tempProcessFormList)
          let index = 0; let filterArray = []
          for (const taskForm of tempProcessFormList) {
            let enableBtnsObj = findTreeNodeForFSG([JSON.parse(taskForm.formConfs)], 'name', 'curRootFormVisible') 
            let curNodeIsAdditionalObj = findTreeNodeForFSG([JSON.parse(taskForm.formConfs)], 'name', 'curNodeIsAdditional') 
            if (enableBtnsObj && enableBtnsObj.defaultValue === false) {
              console.log('taskForm not visiable index: ', index)
              filterArray.push(index)
            } else if (curNodeIsAdditionalObj) {
              if (taskForm.variables && taskForm.variables.curNodeIsAdditional && taskForm.variables.curNodeIsAdditional === 'N') {
                console.log('taskForm visiable, but curNodeIsAdditional=Y index: ', index)
                filterArray.push(index)
              }
            }
            index++
          }
          tempProcessFormList = tempProcessFormList.filter((item, index, arr) => {
            return !filterArray.some(v => { return v === index })
          })

          // 补充材料申请信息过滤
          const applyFormNameTag = '申请信息v1';
          if (tempProcessFormList && tempProcessFormList.some(taskForm => { return taskForm.title && taskForm.title.indexOf(applyFormNameTag) > 0 })) {
            index = 0; filterArray = []
            for (const taskForm of tempProcessFormList) {
              if (taskForm.title && taskForm.title.indexOf(applyFormNameTag) > 0) {
                filterArray.push(index)
              }
              index++
            }
            const supplementaryMaterialsCondition = data.historyProcNodeList && Array.isArray(data.historyProcNodeList) && data.historyProcNodeList[data.historyProcNodeList.length-1].activityName === '补充材料' && this.processed && routerType !== '2';
            if (filterArray.length > 1) {
              if (supplementaryMaterialsCondition) {
              } else {
                filterArray.pop()
              }
            } else {
              if (supplementaryMaterialsCondition) {
              } else {
                filterArray = []
              }
            }
            tempProcessFormList = tempProcessFormList.filter((item, index, arr) => {
              return !filterArray.some(v => { return v === index })
            })
          }

          // 办理信息过滤
          const operateFormNameTag = '办理信息v1';
          if (tempProcessFormList && tempProcessFormList.some(taskForm => { return taskForm.title && taskForm.title.indexOf(operateFormNameTag) > 0 })) {
            index = 0; filterArray = []
            for (const taskForm of tempProcessFormList) {
              if (taskForm.title && taskForm.title.indexOf(operateFormNameTag) > 0) {
                filterArray.push(index)
              }
              index++
            }
            const operateCondition = data.historyProcNodeList && Array.isArray(data.historyProcNodeList) && data.historyProcNodeList[data.historyProcNodeList.length-1].activityName === '操作工单' && this.processed && routerType !== '2';
            if (filterArray.length > 1) {
              if (operateCondition) {
              } else {
                filterArray.pop()
              }
            } else {
              if (operateCondition) {
              } else {
                filterArray = []
              }
            }
            tempProcessFormList = tempProcessFormList.filter((item, index, arr) => {
              return !filterArray.some(v => { return v === index })
            })
          }

          // 表单倒序
          if (tempProcessFormList) {
            tempProcessFormList = tempProcessFormList.slice().reverse()
            this.processFormList = tempProcessFormList
          }
          console.log('getProcessDetails this.processFormList reversed: ', this.processFormList)
        }
        this.taskFormOpen = data.existTaskForm;
        if (this.taskFormOpen) {
          this.taskFormData = data.taskFormData;
        } else if (data.taskVFormData) {
          this.taskFormOpen = true
          this.taskFormData = data.taskVFormData
        }
        this.historyProcNodeList = data.historyProcNodeList;
        this.finishedInfo = data.flowViewer;
        // 增加 comment（审批意见信息展示）, by gaogm
        if (this.taskForm) {
          this.taskForm.comment = data.comment === '   ' ? '' : data.comment
        }
        this.formOpen = true

        // 办理操作按钮权限
        if (this.processed) {
          this.$nextTick(() => {
            if (this.$refs['CptSelfFormInput']) {
              this.curTaskOperateBtnConfig = this.$refs['CptSelfFormInput'].getCurTaskOperateBtnConfig()
              if (this.curTaskOperateBtnConfig.curRootFormVisible === false) {
                this.taskFormOpen = false
              }
              // 详情数据传递到form表单组件
              this.$refs['CptSelfFormInput'].transSelfFormData(data)
            }
          })
        }
      })
    }
  }
}